package com.kgc.community.controller;


import com.github.pagehelper.PageInfo;
import com.kgc.community.mapper.BuildingMapper;
import com.kgc.community.pojo.*;
import com.kgc.community.service.RoomsService;
import com.kgc.community.service.UserService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.lang.Exception;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

@Controller
public class RoomsController {

    @Autowired
    RoomsService roomsService;



    @RequestMapping("homelist.do")
    public String selectAllRoomsVO(HttpServletRequest request, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, Model model){
        Integer pageSize = 10;
        RoomsExample example = new RoomsExample();
        String selectBy=request.getParameter("selectBy")==null?"":request.getParameter("selectBy");
        PageInfo<Rooms> pageInfo = roomsService.selectAllRoomsVO("%"+selectBy+"%",pageNum, pageSize);

        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("selectBy", selectBy);
        return "homelist";
    }

    /**
     * 进入添加页面
     * @return
     */
    @RequestMapping("inAdd.do")
    public String inAdd(Model model){

        List<Rooms> rooms = roomsService.selectRoomsId();
        model.addAttribute("rooms", rooms);
        return "homeadd";
    }

    /**
     * 添加
     * @return
     */
    @RequestMapping("add2.do")
    public String add(HttpServletRequest request, ModelMap model,HttpSession session){

        Map<String, String[]> map = request.getParameterMap();
        User user = new User();
        Rooms rooms = new Rooms();
        Building building=new Building();
        try {
            BeanUtils.populate(user, map);
            BeanUtils.populate(rooms, map);
            BeanUtils.populate(building,map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Integer userId=(int)System.currentTimeMillis();
        Integer buildingId=(int)System.currentTimeMillis();
        Integer status = Integer.parseInt(request.getParameter("status"));
//        String checkingTime = request.getParameter("checkingTime");
//        Integer count = Integer.parseInt(request.getParameter("count"));
//        String name=request.getParameter("name");
//        String tel=request.getParameter("tel");
//        Double area=Double.parseDouble(request.getParameter("area"));
        user.setId(userId);
        building.setId(buildingId);
        rooms.setUserId(userId);
        rooms.setStatus(status);
        rooms.setBuildingId(buildingId);
//        user.setCheckingTime(checkingTime);
//        rooms.setCount(count);
//        user.setName(name);
//        user.setTel(tel);
//        rooms.setArea(area);
        int count1 = roomsService.add(rooms,user,building);
        if(count1>0){
            model.addAttribute("modifyMsg","成功！");
        }else {
            model.addAttribute("modifyMsg","失败！");
        }
        return "forward:homelist.do";

    }

    /**
     * 修改
     */
    @RequestMapping("selecthomelist.do")
    public String modify(HttpServletRequest request, Model model) {
        /*房间号select*/
        List<Rooms> rooms = roomsService.selectRoomsId();
        model.addAttribute("rooms", rooms);
        /*查询其他信息*/
        String roomsid=request.getParameter("id");
        Integer roomUserId = Integer.parseInt(request.getParameter("id"));
        Integer buildingId = Integer.parseInt(request.getParameter("id"));
//        RoomUser roomUser = personnelService.selectPersonnel(roomUserId);
        Rooms rooms1=roomsService.selectRooms(roomsid);
        model.addAttribute("rooms1", rooms1);
        return "homeadd2";
    }

    @RequestMapping("homeadd2.do")
    public String modify(HttpServletRequest request, Model model,  HttpSession session){
//        Map<String, String[]> map = request.getParameterMap();
//        User user = new User();
//        RoomUser roomUser = new RoomUser();
//        try {
//            BeanUtils.populate(user, map);
//            BeanUtils.populate(roomUser, map);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        Map<String, String[]> map = request.getParameterMap();
        User user = new User();
        Rooms rooms = new Rooms();
        Building building=new Building();
        try {
            BeanUtils.populate(user, map);
            BeanUtils.populate(rooms, map);
            BeanUtils.populate(building,map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Integer status = Integer.parseInt(request.getParameter("status"));
        Integer buildingId = Integer.parseInt(request.getParameter("buildingId"));
        Integer userId = Integer.parseInt(request.getParameter("userId"));
        user.setId(userId);
        building.setId(buildingId);
        rooms.setUserId(userId);
        rooms.setBuildingId(buildingId);
        rooms.setStatus(status);
        int count = roomsService.modifyRooms(rooms,user,building);
        if(count>0){
            model.addAttribute("modifyMsg","成功！");
        }else {
            model.addAttribute("modifyMsg","失败！");
        }
        return "forward:homelist.do";

    }


    /**
     * 删除单条
     * @return
     */
    @RequestMapping("delete.do")
    @ResponseBody
    public String deleteRooms(HttpServletRequest request) {
        String id=request.getParameter("id");
        int count =roomsService.deleteRooms(id);
        if (count > 0) {
            return "{\"status\":\"1\"}";
        }
        return "{\"status\":\"2\"}";

    }

    /**
     * 批量删除
     * @return
     */
    @RequestMapping("deleteAll2.do")
    public String deleteAll(HttpServletRequest request, Model model) {
        String[] subs = request.getParameterValues("sub");
        int sum = 0;
        int count = 0;
        for (String sub : subs) {
            String id = request.getParameter(sub);
            count = roomsService.deleteRooms(id);
            sum += count;
        }
        if (sum == subs.length & sum > 0) {
            model.addAttribute("deleteAllresult", true);
            return "forward:homelist.do";
        }
        model.addAttribute("deleteAllresult", false);
        return "forward:homelist.do";
    }

}
